import Vue from 'vue'
import Vuex from 'vuex'

//引入封装好的模块
import menu from "./modules/menu/index"
import role from "./modules/role/index"
import admin from "./modules/admin/index"
import shop from "./modules/shop/shop"
import specs from "./modules/specs/index"
import goods from "./modules/goods/index"
import charts from "./modules/charts/index"
import vvip from "./modules/vvip/index"
import seckill from "./modules/seckill/index"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // userinfo:null
    userinfo: sessionStorage.getItem('userinfo') ? JSON.parse(sessionStorage.getItem('userinfo')) : null
  },
  getters: {
    getUser(state) {
      return state.userinfo
    }
  },
  mutations: {
    REQ_USERINFO(state, payload) {
      if (payload) {
        state.userinfo = payload
        //把结果存储在会话存储中
        sessionStorage.setItem('userinfo', JSON.stringify(payload))
      } else {
        //清空存储(移除)
        sessionStorage.removeItem('userinfo')
      }
    }
  },
  actions: {
    changeUserAction({ commit }, payload) {
      commit('REQ_USERINFO', payload)
    }
  },
  modules: {
    menu,
    role,
    admin,
    shop,
    specs,
    goods,
    charts,
    vvip,
    seckill
  }
})
